package turritopsis

import (
	"ebbflow/pkg/adkr"
	"log"
)

// UseADKRKeyRefresh configures KeyRefreshFunc using a provided constructor that yields KeyRefreshOne.
// This keeps ADKR decoupled; callers decide how to build the KeyRefreshFunc (channels, processes, etc.).
func UseADKRKeyRefresh(makeKR func() KeyRefreshFunc) KeyRefreshFunc {
	return makeKR()
}

// MakeSingleRoundKRViaHigh builds a KeyRefreshFunc that internally constructs one round via ADKRHighProtocol
// using the provided high-level config template and shared message channels.
// Note: This uses ADKR internals but is optional; users can supply their own implementation instead.
func MakeSingleRoundKRViaHigh(
	base adkr.ADKRHighConfig,
	msgInCh <-chan adkr.Message,
	msgOutCh chan<- adkr.Message,
	logger *log.Logger,
) KeyRefreshFunc {
	return func(round int) (*adkr.KeyRefreshOutput, error) {
		// Delegate to the already exposed helper in glue (via KeyRefreshConfig path) if desired.
		// Here we fall back to per-round KeyRefreshConfig using current/next configs from base.
		builder := func(r int) adkr.KeyRefreshConfig {
			return adkr.KeyRefreshConfig{
				SID:         base.SID + "-kr",
				PID:         base.PID,
				N:           base.N,
				F:           base.F,
				L:           base.F,
				ConfigChain: [][]int{makeIndexRange(base.N), makeIndexRange(base.N)},
				G:           base.G1,
				Type:        "b",
				SPK2s:       base.THPKs,
				SSK2:        base.THSK,
				EPKs:        base.EPKs,
				ESK:         base.ESK,
				THSKOld:     base.THSK,
				THPKOld:     base.THPK,
				THPKsOld:    base.THPKs,
			}
		}
		return NewKeyRefreshFuncUsingChannels(builder, msgInCh, msgOutCh, logger)(round)
	}
}

func makeIndexRange(n int) []int {
	idx := make([]int, n)
	for i := 0; i < n; i++ { idx[i] = i }
	return idx
}





